﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Artem.Google.UI;
using System.IO;
using System.Drawing;
using JobSeek.Classes;
using System.Web.Services;
namespace JobSeek.Poster
{
    public partial class JobDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //captcha only serve localhost. Change the public and private key if changing domain names
            if (Page.IsValid)
            {
                job_seekerEntities ctx = new job_seekerEntities();
                
                string selectedLogo = ((HiddenField)Upload1.FindControl("radioSelectedName")).Value;

                Job job = new Job();
                if (Session["logos"] != null)
                {
                    List<Logo> logos = (List<Logo>)Session["logos"];
                    //should we set a maximum number of logo? 
                    foreach (Logo logo in logos)
                    {
                        if (logo.name == selectedLogo)
                        {
                            logo.isUsed = true;

                        }
                        job.Logoes.Add(logo);
                    }
                }
                CustomValidator cust = new CustomValidator();
                RequiredFieldValidator t = new RequiredFieldValidator();
                job.job_title = tbxJobTitle.Text;
                job.main_desc = tbxMainDesc.Text;
                job.short_desc = tbxShortDesc.Text;
                job.contact_details = tbxContact.Text;
                job.pay_struct = ddlPayStruct.SelectedValue;
                

                if (!String.IsNullOrEmpty(tbxVideo_url.Text))
                {
                    job.video_url = tbxVideo_url.Text;
                }

                job.classification_type_id = Convert.ToInt32(ddlClassification.SelectedValue);
                job.location_id = Convert.ToInt32(ddlLocation.SelectedValue);
                if (ddlLocation.SelectedValue != "0")
                {
                    job.sub_location_id = Convert.ToInt32(ddlSubLocation.SelectedValue);
                }

                job.salaryFrom = Decimal.Parse(ddlSalaryRangeFrom.SelectedValue.Replace("$", ""));
                job.salaryTo = Decimal.Parse(ddlSalaryRangeTo.SelectedValue.Replace("$", ""));

                job.work_type = ddlWorkType.SelectedItem.Text;
                job.visible_salary = tbxSalaryRange.Text;

                //beware, must solve this hidden field tampering before launch.
                job.coordinates = hidLatLng.Value;

                job.selling_pt1 = String.IsNullOrEmpty(tbxPt1.Text) ? "" : tbxPt1.Text;
                job.selling_pt2 = String.IsNullOrEmpty(tbxPt2.Text) ? "" : tbxPt2.Text;
                job.selling_pt3 = String.IsNullOrEmpty(tbxPt3.Text) ? "" : tbxPt3.Text;

                Session["jobAds"] = job;

             Response.Redirect("~/Poster/Register.aspx");
            }
            else
            {
                //ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "CaptchaReload", "Recaptcha.reload();", true);
                if (Session["jobAds"] != null)
                    Session["jobAds"] = null;
            }


        }

        protected void ddlPayStruct_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<ListItem> annualList = (List<ListItem>)Application["annual"];
            List<ListItem> hourlyList = (List<ListItem>)Application["hourly"];
            ddlSalaryRangeTo.Items.Clear();
            ddlSalaryRangeTo.Enabled = false;

            tbxSalaryRange.Text = "";
            switch (ddlPayStruct.SelectedItem.Value)
            {
                case "annual":
                    ddlSalaryRangeFrom.DataSource = annualList;

                    ddlSalaryRangeFrom.DataBind();
                    break;

                case "hourly":
                    ddlSalaryRangeFrom.DataSource = hourlyList;
                    ddlSalaryRangeFrom.DataBind();
                    break;

                case "commision":
                    ddlSalaryRangeFrom.DataSource = annualList;
                    ddlSalaryRangeFrom.DataBind();
                    break;

                case "annualComm":
                    ddlSalaryRangeFrom.DataSource = annualList;
                    ddlSalaryRangeFrom.DataBind();
                    break;

                default:
                    break;
            }
            //set the textbox with the amount
        }

        protected void ddlSalaryRangeFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = ddlSalaryRangeFrom.SelectedValue.Replace("$", "");
            tbxSalaryRange.Text = ddlSalaryRangeFrom.SelectedValue;
            decimal realValue = Decimal.Parse(value);

            //selected range is only a few thousands more. 40k difference

            List<ListItem> annualList = (List<ListItem>)Application["annual"];
            List<ListItem> hourlyList = (List<ListItem>)Application["hourly"];

            if (ddlPayStruct.SelectedItem.Value == "annual" || ddlPayStruct.SelectedItem.Value == "commision" || ddlPayStruct.SelectedItem.Value == "annualComm")
            {
                decimal maxValue = realValue + 20000;


                var results = annualList.Where(f => Decimal.Parse(f.Value) >= realValue && Decimal.Parse(f.Value) <= maxValue);

                ddlSalaryRangeTo.DataSource = results;
                ddlSalaryRangeTo.DataBind();

            }
            else
            {
                decimal maxValue = realValue + 30;


                var results = hourlyList.Where(f => Decimal.Parse(f.Value) >= realValue && Decimal.Parse(f.Value) <= maxValue);

                ddlSalaryRangeTo.DataSource = results;
                ddlSalaryRangeTo.DataBind();
            }
            ddlSalaryRangeTo.Enabled = true;
        }

        protected void ddlSalaryRangeTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbxSalaryRange.Text = ddlSalaryRangeFrom.Text + "  -  " + ddlSalaryRangeTo.Text;

        }
    }
}